package top.byfree.jbyan.saber.common

import java.io.Serializable

/**
 * 统一Ajax返回结果
 * @author jbyan
 * @since 21/12/2023
 */
data class R<out T>(
    val code: Int,
    val success: Boolean,
    val message: String,
    val data: T?
) : Serializable


fun <T> ok(code: Int = 200, success: Boolean = true, message: String = "成功", data: T? = null): R<T> {
    return R(code, success, message, data)
}

fun <T> fail(code: Int = 500, success: Boolean = false, message: String = "失败", data: T? = null): R<T> {
    return R(code, success, message, data)
}

fun ok(code: Int = 200, success: Boolean = true, message: String = "成功"): R<Nothing> {
    return R(code, success, message, null)
}

fun fail(code: Int = 500, success: Boolean = false, message: String = "失败"): R<Nothing> {
    return R(code, success, message, null)
}

fun result(flag: Boolean): R<Nothing> {
    return if (flag) ok() else fail()
}
